<template>
  <div class="app-container">
    <template v-if="globalAdmin">
      <ht-search-form
        :dictMap="dictMap"
        :queryParams="queryParams"
        :outputQueryParams="queryParams"
        :formOptions="formOptions"
        :advOptions="[]"
        permission="user:tenantInfo:view"
      ></ht-search-form>
    </template>

    <ht-table
      ref="dataTable"
      :dictMap="dictMap"
      :queryParams="queryParams"
      :tableBindColumns="tableBindColumns"
      :opButtonList="opButtonList"
      :reqConfig="reqConfig"
    >
    </ht-table>

      <edit-tenant
        ref="editTenantDialog"
        @editComplete="changeTypeInfoOpen"
      />

  </div>
</template>

<script>
import { apiUrlConfig,listTenantInfo } from "@/api/authority_app/tenant/tenantInfo";
import { listPageMixin } from "@/libs/listPageMixin";
import EditTenant from "@/views/system/tenantInfo/components/EditTenant";
import {mapGetters} from "vuex";

export default {
  name: "loginInfo",
  mixins: [listPageMixin],
  components: {
    EditTenant
  },
  computed: {
    ...mapGetters(["globalAdmin"]),
  },
  data() {
    return {
      // 查询参数
      queryParams: {
        orderByColumn: "",
        isAsc: "asc",
        tenantCode: "",
        tenantName: ""
      },
      formOptions: [],
      opButtonList: () => {
        return [
          {
            label: `${ this.lang('common.add') }`,
            type: "primary",
            icon: "el-icon-plus",
            permission: "user:tenantInfo:add",
            clickFun: () => {
              this.handleAction("add");
            }
          }
        ];
      },
      reqConfig: apiUrlConfig,
      // 列信息
      tableBindColumns: [
        { label: `${ this.lang('authority.tenantInfo.form.tenantCode') }`, prop: "tenantCode" },
        { label: `${ this.lang('authority.tenantInfo.form.tenantName') }`, prop: "tenantName" },
        { label: `${ this.lang('authority.tenantInfo.form.tenantScopes') }`, prop: "tenantScopes", sortable: false },
        { label: `${ this.lang('authority.tenantInfo.form.leader') }`, prop: "leader" },
        { label: `${ this.lang('authority.tenantInfo.form.phone') }`, prop: "phone" },
        { label: `${ this.lang('authority.tenantInfo.form.email') }`, prop: "email" },
        { label: `${ this.lang('authority.tenantInfo.form.createBy') }`, prop: "createBy" },
        { label: `${ this.lang('authority.tenantInfo.form.createTime') }`, prop: "createTime" },
        {
          label: `${ this.lang('common.operate') }`, type: 'operate', buttonList:  (row) => [
            {
              label: `${ this.lang('common.edit') }`,
              permission: 'user:tenantInfo:edit',
              doFunc: (rowData) => {
                this.handleAction('update', rowData)
              }
            }
          ]
        }
      ],
      //子项目弹出层标题
      titleChild: "",
      // 是否显示子项目弹出层
      openChild: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],

      // 表单参数
      form: {},
      // 子项目表单
      formChild: {},
      // 表单校验
      rules: {},
      curTenantId: "",
      disabled: true
    };
  },
  created() {
    this.getDicts("sys_yes_no").then(response => {
      this.statusOptions = response.result;
    });
  },
  mounted() {
    const formOptions = [
        {
          type: "input",
          prop: "tenantCode",
          defaultAttr: {
            label: `${ this.lang('authority.tenantInfo.form.tenantCode') }`,
            placeholder: `${ this.lang('authority.tenantInfo.rules.tenantCodeEmpty') }`
          },
        },
      {
        type: "input",
        prop: "tenantName",
        defaultAttr: {
          label: `${ this.lang('authority.tenantInfo.form.tenantName') }`,
            placeholder: `${ this.lang('authority.tenantInfo.rules.tenantNameEmpty') }`
        },
      }
    ];
    this.formOptions = formOptions;
  },
  methods: {
    listReq(pageParams) {
      return listTenantInfo(
        this.addDateRange(this.queryParams, this.dateRange)
      );
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //一级弹窗状态修改
    changeTypeInfoOpen(state) {
      if (state) {
        this.doQuery()
      }
    },

    /***
     * 按钮操作汇总
     */
    handleAction(type, row = {}) {
      switch (type) {
        case "add":
          this.$refs.editTenantDialog.show('',true)
          break;
        case "del":
          break;
        case "update":
          this.$refs.editTenantDialog.show(row.id,false)
          break;
      }
    }
  }
};
</script>
